/*
package vip.rssc.commons.data.source.sharding.utils;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.security.Key;
import java.util.Arrays;
import java.util.Optional;


*/
/**
 * <p>Description: [AesUtils]</p >
 * Created on 2020年10月25日
 *
 * @author <a href="1014376159@qq.com">CJ</a>
 * @version 1.0
 * Copyright (c) 2020 cj
 *//*

public class AesUtils {

    private AesUtils() {
    }

    private static final String ALGORITHM = "AES";
    private static final String CHARSET_NAME = "UTF-8";

    public static String decrypt(String data) throws UnsupportedEncodingException, GeneralSecurityException {
        String decryptData = decrypt(data, null);
        return decryptData;
    }

    public static String encrypt(String data) throws UnsupportedEncodingException, GeneralSecurityException {
        return encrypt(data, null);
    }

    public static String encrypt(String data, String key)
            throws GeneralSecurityException, UnsupportedEncodingException {
        key = getSecrtKey(key);
        Key k = toKey(Base64.decodeBase64(key));
        byte[] raw = k.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(1, secretKeySpec);
        byte[] bytes = cipher.doFinal(data.getBytes(CHARSET_NAME));
        return Base64.encodeBase64String(bytes);
    }

    public static String decrypt(String data, String key)
            throws GeneralSecurityException, UnsupportedEncodingException {
        key = getSecrtKey(key);
        Key k = toKey(Base64.decodeBase64(key));
        byte[] raw = k.getEncoded();
        SecretKeySpec secretKeySpec = new SecretKeySpec(raw, ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(2, secretKeySpec);
        byte[] baseData = Base64.decodeBase64(data);
        if (baseData.length == 0) {
            throw new RuntimeException("数据无法通过Base64解码");
        }
        byte[] bytes = cipher.doFinal(baseData);
        return new String(bytes, CHARSET_NAME);
    }

    private static Key toKey(byte[] key) {
        SecretKey secretKey = new SecretKeySpec(key, ALGORITHM);
        return secretKey;
    }

    private static String getSecrtKey(String encrypted) throws UnsupportedEncodingException {
        encrypted = Optional.ofNullable(encrypted).orElse(System.class.getName());
        byte[] bytes = encrypted.getBytes(CHARSET_NAME);
        bytes = Arrays.copyOf(bytes, 16);
        return Base64.encodeBase64String(bytes);
    }

    public static void main(String[] args) throws Throwable {

        String data = "";

        String encryptData = encrypt(data);

        System.out.println(encryptData);

        System.out.println("加密后数据" + encryptData);

        System.out.println("解密后数据" + decrypt(encryptData));

        encryptData = encrypt(data, "abc");

        System.out.println("加密后数据" + encryptData);

        System.out.println("解密后数据" + decrypt(encryptData, "abc"));

    }

}
*/
